﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Commmon.UI
{
    public partial class FrmErrorMessage : Form
    {
        private int maxByteCount;

        public FrmErrorMessage()
        {
            InitializeComponent();
        }

        public FrmErrorMessage(Exception ex)
            : this()
        {
            string message = ex.Message;
            int index = message.IndexOf("请检索");
            if (index >= 0)
            {
                message = message.Substring(index + 3);
                index = message.IndexOf("属性");
                message = message.Substring(0, index).Trim();
            }
            else
            {
                message = "";
            }
            object obj2 = null;
            if (message != "")
            {
                obj2 = ex.GetType().GetProperty(message).GetValue(ex, null);
            }
            string msg = "异常: ";
            if (ex.InnerException != null)
            {
                string str3 = msg;
                msg = str3 + "{" + ex.Message + ex.InnerException.Message + "}";
            }
            else
            {
                msg = msg + "异常: {" + ex.Message + "}";
            }
            this.maxByteCount = this.GetByteCount(msg);
            int byteCount = 0;
            this.tbMore.Text = msg + "\r\n";
            if (obj2 != null)
            {
                Exception[] exceptionArray = (Exception[])obj2;
                for (int i = 0; i < exceptionArray.Length; i++)
                {
                    msg = "      {" + exceptionArray[i].Message + "}";
                    byteCount = this.GetByteCount(msg);
                    if (byteCount > this.maxByteCount)
                    {
                        this.maxByteCount = byteCount;
                    }
                    this.tbMore.Text = this.tbMore.Text + msg + "\r\n";
                }
            }
            msg = "源: " + ex.Source;
            byteCount = this.GetByteCount(msg);
            if (byteCount > this.maxByteCount)
            {
                this.maxByteCount = byteCount;
            }
            this.tbMore.Text = this.tbMore.Text + msg + "\r\n";
            msg = "堆栈:\r\n" + ex.StackTrace;
            byteCount = this.GetByteCount(msg);
            if (byteCount > this.maxByteCount)
            {
                this.maxByteCount = byteCount;
            }
            this.tbMore.Text = this.tbMore.Text + msg + "\r\n";
            msg = "方法: " + ex.TargetSite;
            byteCount = this.GetByteCount(msg);
            if (byteCount > this.maxByteCount)
            {
                this.maxByteCount = byteCount;
            }
            this.tbMore.Text = this.tbMore.Text + msg + "\r\n";
            if (ex.InnerException != null)
            {
                this.tbMessage.Text = ex.Message + "\r\n" + ex.InnerException.Message;
            }
            else
            {
                this.tbMessage.Text = ex.Message;
            }
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(this.tbMore.Text);
        }

        private void btnMore_Click(object sender, EventArgs e)
        {
            if (this.btnMore.Tag.ToString() == "y")
            {
                this.btnMore.Tag = this.tbMore.Height;
                this.btnMore.Text = "详细信息▼";
                base.Height -= this.tbMore.Height;
                base.Width = 0x13c;
                this.btnCopy.Visible = false;
                this.ssInform.Visible = false;
                base.FormBorderStyle = FormBorderStyle.FixedSingle;
            }
            else
            {
                this.btnMore.Text = "详细信息▽";
                if (this.btnMore.Tag.ToString() == "0")
                {
                    base.Height += 0x9e;
                }
                else
                {
                    base.Height += Convert.ToInt32(this.btnMore.Tag.ToString());
                }
                this.btnMore.Tag = "y";
                this.btnCopy.Visible = true;
                this.ssInform.Visible = true;
                base.FormBorderStyle = FormBorderStyle.Sizable;
                base.Width = (this.maxByteCount * 6) + 50;
            }
            base.Left = (Screen.PrimaryScreen.Bounds.Width - base.Width) / 2;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.OK;
        }

        private void FrmInform_Shown(object sender, EventArgs e)
        {
            this.btnMore.PerformClick();
        }

        private void FrmInform_SizeChanged(object sender, EventArgs e)
        {
            if (base.Width < 0x192)
            {
                base.Width = 0x192;
            }
            if (base.Height < 0x7c)
            {
                base.Height = 0x9e;
            }
        }

        private int GetByteCount(string msg)
        {
            int num = 0;
            int num2 = 0;
            int index = -1;
            do
            {
                index = msg.IndexOf("\r\n");
                if (index >= 0)
                {
                    num2 = 0;
                    for (int j = 0; j < index; j++)
                    {
                        if (Convert.ToInt32(msg[j]) < Convert.ToInt32(Convert.ToChar(0x80)))
                        {
                            num2++;
                        }
                        else
                        {
                            num2 += 2;
                        }
                    }
                    if (num2 > num)
                    {
                        num = num2;
                    }
                    msg = msg.Substring(index + 2);
                }
            }
            while (index >= 0);
            num2 = 0;
            for (int i = 0; i < msg.Length; i++)
            {
                if (Convert.ToInt32(msg[i]) < Convert.ToInt32(Convert.ToChar(0x80)))
                {
                    num2++;
                }
                else
                {
                    num2 += 2;
                }
            }
            if (num2 > num)
            {
                num = num2;
            }
            return num;
        }


        private void panel3_SizeChanged(object sender, EventArgs e)
        {
            this.btnOk.Left = (this.panel3.Width - this.btnOk.Width) / 2;
        }

    }
}
